<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="时间范围">
        <el-date-picker v-model="selectTimeRanges" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 340px;"></el-date-picker>
      </el-form-item>
      <el-form-item label="昵称" prop="productCode">
        <el-input v-model="queryParams.name" @keyup.enter.native="handleQuery" placeholder="请输入昵称" style="width: 100%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" ref="toolbar">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="总接取数量" align="center" prop="count" />
      <el-table-column label="未完成数量" align="center" prop="unfinished_count" />
      <el-table-column label="已完成数量" align="center" prop="finished_count" />
    </el-table>
  </div>
</template>

<script>
import ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
import {parseTime} from "@/utils/ruize";
import { getTaskCompletionStatistics } from "@/api/business/received";

export default {
  name: "Taskerp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 任务erp表格数据
      list: [],
      queryParams: {
        beginTime: null,
        endTime: null
      },
      selectTimeRanges: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.selectTimeRanges.length >= 0){
        this.queryParams.beginTime = parseTime(this.selectTimeRanges[0], '{y}-{m}-{d}')
        this.queryParams.endTime = parseTime(this.selectTimeRanges[1], '{y}-{m}-{d}')
      }else {
        this.queryParams.beginTime = null
        this.queryParams.endTime = null
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.selectTimeRanges = []
      this.queryParams.beginTime = null
      this.queryParams.endTime = null
      this.queryParams.name = null
      this.handleQuery();
    },
    /** 查询任务erp列表 */
    getList() {
      this.loading = true;
      getTaskCompletionStatistics(this.queryParams).then(response => {
        const roles = this.$store.getters.roles.join()
        const moban = response.data.filter(row => row.post === "模版");
        const meigong = response.data.filter(row => row.post === "美工");
        const pinban = response.data.filter(row => row.post === "拼版");
        const sku = response.data.filter(row => row.post === "sku");
        if (roles.includes('admin') || roles.includes('administrator') || roles.includes('meigongzhuguan') || roles.includes('zongjian')){
          this.list = [ ...moban, ...meigong, ...pinban , ...sku ]
        }else if (roles.includes('mobanzuzhang')){
          this.list = [ ...moban, ...pinban ]
        }else if (roles.includes('meigongzuzhang')){
          this.list = [ ...meigong, ...sku ]
        }
        this.loading = false;
      });
    },
    async handleExport(){
      const workbook = new ExcelJS.Workbook();
      const worksheet = workbook.addWorksheet('My Sheet');

      // 设置表头
      worksheet.columns = [
        { header: '姓名', key: 'name', width: 30 },
        { header: '岗位', key: 'post', width: 15 },
        { header: '总接取数量', key: 'count', width: 10 },
        { header: '未完成数量', key: 'unfinished_count', width: 10 },
        { header: '已完成数量', key: 'finished_count', width: 10 },
      ];

      // 添加行数据
      this.list.forEach(item => {
        worksheet.addRow({
          name: item.name,
          post: item.post,
          count: item.count,
          unfinished_count: item.unfinished_count,
          finished_count: item.finished_count,
        });
      });

      // 写文件
      const buffer = await workbook.xlsx.writeBuffer();
      saveAs(new Blob([buffer], { type: 'application/octet-stream' }), new Date().getTime() + ".xlsx");
    },
  }
};
</script>
